import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Cards {
    public static final String suits[] = {"♣", "♥", "♦", "♣"};

    //创建初始牌堆
    public List<Card> creatCard() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 2; j <= 14; j++) {
                int rank = j;
                String suit = suits[i];
                Card card = new Card(rank, suit);
                cardList.add(card);
            }
        }
        return cardList;
    }


    //模拟洗牌
    public List<Card> shuffle(List<Card> cardList){
        Random random=new Random();
        for (int i =cardList.size()-1 ; i >0; i--) {
            int temp= random.nextInt(i);
            swap(cardList,temp,i);
        }
        return cardList;
    }

    public void swap(List<Card> cardList,int i,int j){
        Card temp=cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,temp);
    }

    //模拟发牌
    public void deaqlCards(List<Card> cardList){
        List<Card> hand1=new ArrayList<>();
        List<Card> hand2=new ArrayList<>();
        List<Card> hand3=new ArrayList<>();

        List<List<Card>> hands=new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 3; j++) {
                Card card=cardList.remove(0);
                List<Card> temp=hands.get(j);
                temp.add(card);
            }
        }

        System.out.println("第1个人的牌："+hand1);
        System.out.println("第2个人的牌："+hand2);
        System.out.println("第3个人的牌："+hand3);
        System.out.println("牌堆顶剩余的四张牌为："+cardList);

        System.out.println("自动理牌之后呈现为");
        sorthands(hand1);
        sorthands(hand2);
        sorthands(hand3);
        sorthands(cardList);
        System.out.println("第1个人的牌："+hand1);
        System.out.println("第2个人的牌："+hand2);
        System.out.println("第3个人的牌："+hand3);
        System.out.println("牌堆顶剩余的四张牌为："+cardList);
    }

    //模拟理牌
    public void sorthands(List<Card> hand){
        for (int i = 0; i < hand.size()-1; i++) {
            for (int j = 0; j < hand.size()-i-1; j++) {
                Card card1=hand.get(j);
                Card card2=hand.get(j+1);
                if (hand.get(j).rank>hand.get(j+1).rank)
                {
                    Card temp=hand.get(j);
                    hand.set(j,hand.get(j+1));
                    hand.set(j+1,temp);
                }
            }
        }
    }

}